var ajax = {
    
    get : function(settings) {
        settings.method = "GET";
        return ajax.call(settings);
    },
    
    post : function(settings) {
    	settings.method = "POST";
        return ajax.call(settings);
    },
    
    put : function(settings) {
        settings.method = "PUT";
        settings.methodOverride = "PUT";
        return ajax.call(settings);
    },

    del: function(settings) {
        settings.method = "DELETE";
        settings.methodOverride = "DELETE";
        return ajax.call(settings);
    },
    
    call : function(settings) {
    	//method 
    	var method = settings.method;
        if (method == null) {
            method = "GET";
        }
        method = method.toUpperCase();
        
        //xhr
        var xhr;
        if (window.XMLHttpRequest) {
        	xhr= new XMLHttpRequest();
        } else {
        	xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
         xhr.withCredentials = true;
        //url
        var url = settings.url;        
        
        //params
        var parameters = ajax.generateParameter(settings.param);
        
        
        if (method != "POST" && settings.param) {
            if (url.indexOf("?") == -1) {
                url += "?" + parameters;
            } else {
                url += "&" + parameters;
            }
        }
        
        xhr.url = url;
        xhr.requestTimeout = null;
        xhr.onreadystatechange = ajax.callback(xhr, settings);
        
        xhr.open(method, url, false);   
        
        //xhr.setRequestHeader("X-Accept", "text/json-unsecure");
       // xhr.overrideMimeType("application/json");        
        //xhr.setRequestHeader("X-Accept-Charset", "UTF-8");
        
        if (method == "GET") {
        	try {
            	xhr.send(); 
           } catch(xhr) {}
        } else {
        	if(settings.json) {
        		xhr.setRequestHeader("Content-Type","application/json");	
        	} else {
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        	}
            xhr.send(parameters);
        }
        return xhr;
    },
    
    callback : function(xhr, settings) {
    	return function() {
    		if (xhr.readyState == 4) {
		    	var status = xhr.status;
		    	if(status == 200) {
		    		settings.success(xhr.responseXML, xhr);
		    	} else {
		    		settings.failure(xhr);
		    	}
	    	}
    	};
    },
    
    generateParameter : function(param) {
        var p = "";
        var s = "";
        for (var k in param) {

            var value = param[k];

            if (typeof(value) != "function") {

                if (typeof(value) == "undefined" || value == null) {
                    value = "";
                } else if (!value._isArray) {
                    value = [value];
                }

                var l = value.length;
                for (var n = 0; n < l; n ++) {
                    var v = value[n];

                    if (param[k] != null) { // warning : if the value can be equals 0
                        p += (s + k + "=" + ajax.encodeParameter(v));
                    } else {
                        p += (s + k);
                    }
                    s = "&";
                }

                s = "&";
            }
        }

        return p; 
    },
    
    encodeParameter : function(param) {
        var result = null;
        if (typeof param == "string") {
            result = param;
        } else {
            result = JSON.stringify(param);
        }

        result = encodeURIComponent(result);

        return result;
    }
};
    